<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java 基础语法</h1>
        <p>一个Java程序可以认为是一系列对象的集合，而这些对象通过调用彼此的方法来协同工作。下面简要介绍下类、对象、方法和实例变量的概念。</p>
        <ul>
            <li>
                <strong>对象</strong>：对象是类的一个实例，有状态和行为。例如，一条狗是一个对象，它的状态有：颜色、名字、品种；行为有：摇尾巴、叫、吃等。</li>
            <li>
                <strong>类</strong>：类是一个模板，它描述一类对象的行为和状态。</li>
            <li>
                <strong>方法</strong>：方法就是行为，一个类可以有很多方法。逻辑运算、数据修改以及所有动作都是在方法中完成的。</li>
            <li>
                <strong>实例变量</strong>：每个对象都有独特的实例变量，对象的状态由这些实例变量的值决定。</li>
        </ul><hr>
        <h2>第一个Java程序</h2>
        <p>下面看一个简单的Java程序，它将打印字符串<em> Hello World</em></p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">HelloWorld</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-comment">/*</span><span class="hl-comment"> 第一个Java程序
     * 它将打印字符串 Hello World
     </span><span class="hl-comment">*/</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Hello World</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 打印 Hello World</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
            <br>
        </div>
        <p>下面将逐步介绍如何保存、编译以及运行这个程序：</p>
        <ul>
            <li>
                打开Notepad，把上面的代码添加进去；</li>
            <li>
                把文件名保存为：HelloWorld.java；</li>
            <li>
                打开cmd命令窗口，进入目标文件所在的位置，假设是C:\</li>
            <li>
                在命令行窗口键入 javac HelloWorld.java&nbsp; 按下enter键编译代码。如果代码没有错误，cmd命令提示符会进入下一行。（假设环境变量都设置好了）。</li>
            <li>
                再键入java HelloWorld 按下Enter键就可以运行程序了</li>
        </ul>
        <p>你将会在窗口看到 Hello World</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">C </span><span class="pun">:</span><span class="pln"> </span><span class="pun">&gt;</span><span class="pln"> javac </span><span class="typ">HelloWorld</span><span class="pun">.</span><span class="pln">java
C </span><span class="pun">:</span><span class="pln"> </span><span class="pun">&gt;</span><span class="pln"> java </span><span class="typ">HelloWorld</span><span class="pln">
</span><span class="typ">Hello</span><span class="pln"> </span><span class="typ">World</span></pre>
        <p>Gif 图演示：</p>
        <img src="http://www.runoob.com/wp-content/uploads/2013/12/java-HelloWorld.gif">
        <hr>
        <h2>基本语法</h2>
        <p>编写Java程序时，应注意以下几点：</p>
        <ul>
            <li>
                <strong>大小写敏感</strong>：Java是大小写敏感的，这就意味着标识符Hello与hello是不同的。</li>
            <li>
                <strong>类名</strong>：对于所有的类来说，类名的首字母应该大写。如果类名由若干单词组成，那么每个单词的首字母应该大写，例如&nbsp;MyFirstJavaClass 。</li>
            <li>
                <strong>方法名</strong>：所有的方法名都应该以小写字母开头。如果方法名含有若干单词，则后面的每个单词首字母大写。</li>
            <li>
                <strong>源文件名</strong>：源文件名必须和类名相同。当保存文件的时候，你应该使用类名作为文件名保存（切记Java是大小写敏感的），文件名的后缀为.java。（如果文件名和类名不相同则会导致编译错误）。</li>
            <li>
                <strong>主方法入口</strong>：所有的Java 程序由<strong>public static void main(String []args)</strong>方法开始执行。</li>
        </ul>

        <hr>
        <h2>Java标识符</h2>
        <p>Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。</p>
        <p>关于Java标识符，有以下几点需要注意：</p>
        <ul>
            <li>
                所有的标识符都应该以字母（A-Z或者a-z）,美元符（$）、或者下划线（_）开始</li>
            <li>
                首字符之后可以是字母（A-Z或者a-z）,美元符（$）、下划线（_）或数字的任何字符组合</li>
            <li>
                关键字不能用作标识符</li>
            <li>
                标识符是大小写敏感的</li>
            <li>
                合法标识符举例：age、$salary、_value、__1_value</li>
            <li>
                非法标识符举例：123abc、-salary</li>
        </ul><hr>
        <h2>Java修饰符</h2>
        <p>
            像其他语言一样，Java可以使用修饰符来修饰类中方法和属性。主要有两类修饰符：</p>
        <ul>
            <li>访问控制修饰符 : default, public , protected, private</li>
            <li>非访问控制修饰符 : final, abstract, strictfp</li>
        </ul>
        <p>
            在后面的章节中我们会深入讨论Java修饰符。</p><hr>
        <h2>
            Java变量</h2>
        Java中主要有如下几种类型的变量<br>
        <ul>
            <li>局部变量</li>
            <li>类变量（静态变量）</li>
            <li>成员变量（非静态变量）</li>
        </ul><hr>
        <h2>Java数组</h2>
        <p>数组是储存在堆上的对象，可以保存多个同类型变量。在后面的章节中，我们将会学到如何声明、构造以及初始化一个数组。</p><hr>
        <h2>Java枚举</h2>
        <p>Java 5.0引入了枚举，枚举限制变量只能是预先设定好的值。使用枚举可以减少代码中的bug。</p>
        <p>例如，我们为果汁店设计一个程序，它将限制果汁为小杯、中杯、大杯。这就意味着它不允许顾客点除了这三种尺寸外的果汁。</p>

        <h3>实例</h3>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">FreshJuice</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-identifier">enum</span><span class="hl-code"> </span><span class="hl-identifier">FreshJuiceSize</span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">SMALL</span><span class="hl-code">, </span><span class="hl-identifier">MEDIUM</span><span class="hl-code"> , </span><span class="hl-identifier">LARGE</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-identifier">FreshJuiceSize</span><span class="hl-code"> </span><span class="hl-identifier">size</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">FreshJuiceTest</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">FreshJuice</span><span class="hl-code"> </span><span class="hl-identifier">juice</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FreshJuice</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">juice</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-code"> = </span><span class="hl-identifier">FreshJuice</span><span class="hl-code">.</span><span class="hl-identifier">FreshJuiceSize</span><span class="hl-code">.</span><span class="hl-identifier">MEDIUM</span><span class="hl-code">  ;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p><strong>注意：</strong>枚举可以单独声明或者声明在类里面。方法、变量、构造函数也可以在枚举中定义。</p>
        <hr>
        <h2>
            Java关键字
        </h2>
        <p>
            下面列出了Java保留字。这些保留字不能用于常量、变量、和任何标识符的名称。
        </p>
        <table class="reference">
            <tbody><tr>
                <th>类别</th>
                <th>关键字</th>
                <th>说明</th>
            </tr>
            <tr>
                <td rowspan="3" align="center">访问控制</td>
                <td>private</td>
                <td>私有的</td>
            </tr>
            <tr>
                <td>protected</td>
                <td>受保护的</td>
            </tr>
            <tr>
                <td>public</td>
                <td>公共的</td>
            </tr>
            <tr>
                <td rowspan="13" align="center">类、方法和变量修饰符</td>
                <td>abstract</td>
                <td>声明抽象</td>
            </tr>
            <tr>
                <td>class</td>
                <td>类</td>
            </tr>
            <tr>
                <td>extends</td>
                <td>扩允,继承</td>
            </tr>
            <tr>
                <td>final</td>
                <td>最终值,不可改变的</td>
            </tr>
            <tr>
                <td>implements</td>
                <td>实现（接口）</td>
            </tr>
            <tr>
                <td>interface</td>
                <td>接口</td>
            </tr>
            <tr>
                <td>native</td>
                <td>本地，原生方法（非Java实现）</td>
            </tr>
            <tr>
                <td>new</td>
                <td>新,创建</td>
            </tr>
            <tr>
                <td>static</td>
                <td>静态</td>
            </tr>
            <tr>
                <td>strictfp</td>
                <td>严格,精准</td>
            </tr>
            <tr>
                <td>synchronized</td>
                <td>线程,同步</td>
            </tr>
            <tr>
                <td>transient</td>
                <td>短暂</td>
            </tr>
            <tr>
                <td>volatile</td>
                <td>易失</td>
            </tr>
            <tr>
                <td rowspan="12" align="center">程序控制语句</td>
                <td>break</td>
                <td>跳出循环</td>
            </tr>
            <tr>
                <td>case</td>
                <td>定义一个值以供switch选择</td>
            </tr>
            <tr>
                <td>continue</td>
                <td>继续</td>
            </tr>
            <tr>
                <td>default</td>
                <td>默认</td>
            </tr>
            <tr>
                <td>do</td>
                <td>运行</td>
            </tr>
            <tr>
                <td>else</td>
                <td>否则</td>
            </tr>
            <tr>
                <td>for</td>
                <td>循环</td>
            </tr>
            <tr>
                <td>if</td>
                <td>如果</td>
            </tr>
            <tr>
                <td>instanceof</td>
                <td>实例</td>
            </tr>
            <tr>
                <td>return</td>
                <td>返回</td>
            </tr>
            <tr>
                <td>switch</td>
                <td>根据值选择执行</td>
            </tr>
            <tr>
                <td>while</td>
                <td>循环</td>
            </tr>
            <tr>
                <td rowspan="6" align="center">错误处理</td>
                <td>assert</td>
                <td>断言表达式是否为真</td>
            </tr>
            <tr>
                <td>catch</td>
                <td>捕捉异常</td>
            </tr>
            <tr>
                <td>finally</td>
                <td>有没有异常都执行</td>
            </tr>
            <tr>
                <td>throw</td>
                <td>抛出一个异常对象</td>
            </tr>
            <tr>
                <td>throws</td>
                <td>声明一个异常可能被抛出</td>
            </tr>
            <tr>
                <td>try</td>
                <td>捕获异常</td>
            </tr>
            <tr>
                <td rowspan="2" align="center">包相关</td>
                <td>import</td>
                <td>引入</td>
            </tr>
            <tr>
                <td>package</td>
                <td>包</td>
            </tr>
            <tr>
                <td rowspan="9" align="center">基本类型</td>
                <td>boolean</td>
                <td>布尔型</td>
            </tr>
            <tr>
                <td>byte</td>
                <td>字节型</td>
            </tr>
            <tr>
                <td>char</td>
                <td>字符型</td>
            </tr>
            <tr>
                <td>double</td>
                <td>双精度浮点</td>
            </tr>
            <tr>
                <td>float</td>
                <td>单精度浮点</td>
            </tr>
            <tr>
                <td>int</td>
                <td>整型</td>
            </tr>
            <tr>
                <td>long</td>
                <td>长整型</td>
            </tr>
            <tr>
                <td>short</td>
                <td>短整型</td>
            </tr>
            <tr>
                <td>null</td>
                <td>空</td>
            </tr>
            <tr>
                <td rowspan="3" align="center">变量引用</td>
                <td>super</td>
                <td>父类,超类</td>
            </tr>
            <tr>
                <td>this</td>
                <td>本类</td>
            </tr>
            <tr>
                <td>void</td>
                <td>无返回值</td>
            </tr>
            <tr>
                <td rowspan="2" align="center">保留关键字</td>
                <td>goto</td>
                <td>是关键字，但不能使用</td>
            </tr>
            <tr>
                <td>const</td>
                <td>是关键字，但不能使用</td>
            </tr>
            </tbody></table>

        <hr>
        <h2>
            Java注释</h2>
        <p>类似于C/C++，Java也支持单行以及多行注释。注释中的字符将被Java编译器忽略。</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">HelloWorld</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-comment">/*</span><span class="hl-comment"> 这是第一个Java程序
    *它将打印Hello World
    * 这是一个多行注释的示例
    </span><span class="hl-comment">*/</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-comment">//</span><span class="hl-comment"> 这是单行注释的示例</span><span class="hl-comment"></span><span class="hl-code">
       </span><span class="hl-comment">/*</span><span class="hl-comment"> 这个也是单行注释的示例 </span><span class="hl-comment">*/</span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Hello World</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div><hr>
        <h2>Java 空行</h2>
        <p>空白行，或者有注释的行，Java编译器都会忽略掉。</p><hr>

        <h2>
            继承</h2>
        <p>在Java中，一个类可以由其他类派生。如果你要创建一个类，而且已经存在一个类具有你所需要的属性或方法，那么你可以将新创建的类继承该类。</p>
        <p>
            利用继承的方法，可以重用已存在类的方法和属性，而不用重写这些代码。被继承的类称为超类（super class），派生类称为子类（subclass）。</p><hr>
        <h2>
            接口</h2>
        <p>在Java中，接口可理解为对象间相互通信的协议。接口在继承中扮演着很重要的角色。</p>
        <p>
            接口只定义派生要用到的方法，但是方法的具体实现完全取决于派生类。</p>

        <hr>
        <h2>Java 源程序与编译型运行区别</h2>
        <p>如下图所示：</p>
        <p><img src="http://www.runoob.com/wp-content/uploads/2013/12/ZSSDMld.png"></p>
        <p>
            下一节介绍Java编程中的类和对象。之后你将会对Java中的类和对象有更清楚的认识。</p>

    </div>
</div>
</body>
</html>
